package
{
	import flash.display.Sprite;
	import flash.utils.ByteArray;
	import flash.external.ExternalInterface;
	import bsp.BspClient;
	import bsp.utils.Debug;
	import com.adobe.crypto.MD5;
	import com.adobe.serialization.json.JSON;
	
	[SWF(framerate=30, width=1, height=1, background=0xFFFFFF)]
	
	public class bsp_as3 extends Sprite
	{
		private const CMD_LOGIN_REQ : uint			= 10001;
		private const CMD_LOGIN_RES : uint			= 10002;
		private const CMD_CHECK_REQ : uint			= 10003;
		private const CMD_CHECK_RES : uint			= 10004;
		private const CMD_MSG_REQ : uint			= 10005;
		private const CMD_MSG_RES : uint			= 10006;
		private const CMD_STATUS_REQ : uint			= 10007;
		private const CMD_STATUS_RES : uint			= 10008;
		
		private const hash_salt : String			= "sanshi";
		private var bspc : BspClient;
		private var uid : uint						= 0;
		
		public function bsp_as3()
		{
			var p : Object = {
				protocol		: 'Luabase', 
				addr			: '192.168.3.233', 
				port			: 65100, 
				on_connect		: _t_conn, 
				on_close		: _t_close, 
				on_cmd			: _t_cmd, 
				heartbeat		: 1
			};
			this.bspc = new BspClient(p);
			
			if (ExternalInterface.available)
			{
				ExternalInterface.addCallback('im_do_login', _do_login);
				ExternalInterface.addCallback('im_do_msg', _do_msg);
				ExternalInterface.addCallback('im_do_status', _do_status);
				ExternalInterface.addCallback('im_do_check', _do_check);
			}
			
			return;
		}
		
		private function _t_conn() : void
		{
			if (ExternalInterface.available)
			{
				//ExternalInterface.call('cb_im_connect');
				ExternalInterface.call('im.login');
			}
			
			return;
		}
		
		private function _t_close() : void
		{
			if (ExternalInterface.available)
			{
				//ExternalInterface.call('cb_im_disconnect');
			}
			
			return;
		}
		
		private function _t_cmd(cmd : Object) : void
		{
			switch (cmd.cmd_id)
			{
				case CMD_LOGIN_RES : 
					if (ExternalInterface.available)
					{
						//ExternalInterface.call('cb_im_login', JSON.encode(cmd.params));
						ExternalInterface.call('im.lgSuc', JSON.encode(cmd.params));
						//Debug.dump_object(cmd.params);
					}
					
					if (cmd.error_no == 0)
					{
						this.uid = cmd.params.contents.uid;
					}
					
					break;
				
				case CMD_STATUS_RES : 
					if (ExternalInterface.available)
					{
						ExternalInterface.call('im.setStatus', JSON.encode(cmd.params));
						//Debug.dump_object(cmd.params);
					}
					
					break;
				
				case CMD_MSG_RES : 
					if (ExternalInterface.available)
					{
						//ExternalInterface.call('cb_im_msg', JSON.encode(cmd.params));
						ExternalInterface.call('im.readJson', JSON.encode(cmd.params));
						//Debug.dump_object(cmd.params);
					}
					
					break;
				
				case CMD_CHECK_RES : 
					if (ExternalInterface.available)
					{
						//ExternalInterface.call('cb_im_check', JSON.encode(cmd.params));
						ExternalInterface.call('im.showUser', JSON.encode(cmd.params));
						//Debug.dump_object(cmd.params);
					}
					
					break;
				
				default : 
					break;
			}
			
			return;
		}
		
		private function _do_login(uid : uint, nick : String) : void
		{
			var token : String = MD5.hash(this.hash_salt + uid.toString());
			this.bspc.send(CMD_LOGIN_REQ, {user_id:uid, cookie_hash:token, nick:nick});
			
			return;
		}
		
		private function _do_msg(uid : uint, msg : String) : void
		{
			// Send message to another user
			this.bspc.send(CMD_MSG_REQ, {uid:uid, msg:msg});
			
			return;
		}
		
		private function _do_status(status : uint) : void
		{
			// Change status
			this.bspc.send(CMD_STATUS_REQ, {uid:this.uid, status:status});
			
			return;
		}
		
		private function _do_check(uid : uint) : void
		{
			// Check status
			this.bspc.send(CMD_CHECK_REQ, {uid:uid});
			
			return;
		}
	}
}